package com.group525.cloudef.utils;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * mybatisPlus代码生成器
 */
@Component
public class CodeGenerator {

    static String list = "admin,user,doctor";
//
//    @Value("${user.a}")
//    private String database;


    public static void main(String[] args) {
        String module = "";
        String table = "";
        Scanner scanner = new Scanner(System.in);
        System.out.format("\033[34m" + "请从[%s]选择对应的模块输入：\n", list);
        System.out.format("\033[38m" + "请输入模块名: ");
        if (scanner.hasNext()) {
            module = scanner.next();
            while (!list.contains(module)) {
                System.err.print("模块有误! 请重复输入: ");
                module = scanner.next();
            }
        }
        //多个表可以逗号隔开
        System.out.print("请输入表名: ");
        if (scanner.hasNext()) {
            table = scanner.next();
        }
        scanner.close();

        CodeGeneratorRun(module, table);


    }

    public static void CodeGeneratorRun(String module, String table) {
        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/server/src/main/java");
        gc.setAuthor("group525");
        gc.setOpen(false); //生成后是否打开资源管理器
        gc.setFileOverride(false); //重新生成时文件是否覆盖
        /*
         * mp生成service层代码，默认接口名称第一个字母有 I
         * UcenterService
         * */
        gc.setServiceName("%sService");    //去掉Service接口的首字母I
//        gc.setIdType(IdType.ID_WORKER_STR); //主键策略
        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
        gc.setSwagger2(true);//开启Swagger2模式

        mpg.setGlobalConfig(gc);

        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost/cloudef?serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("12345678");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);


        // 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("cloudef"); //模块名
        pc.setParent("com.group525");
        pc.setController("controller." + module);
        pc.setEntity("entity." + module);
        pc.setService("service." + module);
        pc.setServiceImpl("service." + module + ".impl");
        pc.setMapper("mapper." + module);
        pc.setXml("mapper." + module + ".xml");
        mpg.setPackageInfo(pc);

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        //在这儿添加表名
        strategy.setInclude(table);
        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
        strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀

        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符

        mpg.setStrategy(strategy);

        // 6、执行
        mpg.execute();
    }


}
